<?php
/*
Plugin Name: PortalRSS 
Plugin URI: http://www.de-sci.org/blogs
Description: Let mutil-blog editors recommend articles on start pages
Version: 0.10
Author: LIU Xuefeng
Author URI: http://www.de-sci.og
*/


function show_portal_rss($rec_title,$record_num=3,$cat_num=2, $cat_id=NULL ){
	global $wpdb, $blog,$more_article;

	$output="";

	if(!$more_article){
		$qry_cond=" limit 0, $cat_num";
	}
	$cat_cond= " catalogs.parent_cat=\"28067\" ";
	if($cat_id) $cat_cond = " catalogs.cat_id=$cat_id ";

$qry="SELECT catalogs.title, catalogs.alias,catalogs.cat_id, max(product_detail.date) as arc_date
FROM catalogs 
inner join products on products.cat_id=catalogs.cat_id
inner join product_detail on product_detail.product_id=products.product_id
where $cat_cond
group by catalogs.cat_id order by arc_date desc
$qry_cond
";
	$arc_result = $wpdb->get_results($qry);
	foreach($arc_result as $re){

		$id_list="";
		$qry="SELECT product_detail.other as id FROM product_detail inner join products where products.product_id=product_detail.product_id and products.cat_id=\"".$re->cat_id."\" ";
		$arc_re2 = $wpdb->get_results($qry);
		foreach($arc_re2 as $re2){
		   if($re2->id){
			   if($id_list)
				$id_list.=", $re2->id";
			   else
				$id_list.="$re2->id";
		   }
		}

		$title=iconv("gbk","utf-8",$re->title);

		$output.="";
		if($cat_id==$re->cat_id)
		$output.=get_portal_rss($title,$id_list,-1,$re->cat_id);	
		else
		$output.=get_portal_rss($title,$id_list,$record_num,$re->cat_id);	

		
	}	


	$re=$output;

	return $re;
}

function get_portal_rss($title, $id_list, $record_num=20,$cat_id=NULL){

	global $wpdb, $blog,$more_article;

	global $rss_date;


	$rss_date="";



	$hot_title = $title;
	$output="";
/*
	$output.= "<item>\n";
	$output.= "<title> $title (".date("Y-m-d").") </title>\n";
	$output.= "<pubDate>".date("D, d M Y 00:00:00 O"). "</pubDate>\n";
	$output.= "<lastBuildDate>".date("r"). "</lastBuildDate>\n";
	$output.= "</item>\n";
*/

	if($record_num>0) $qry_num=" limit 0,$record_num";

	$my_q="SELECT distinct `$wpdb->posts`.ID, `$wpdb->posts`.post_author as user_id,`$wpdb->posts`.post_name, `$wpdb->posts`.post_title, `$wpdb->posts`.guid, 
$wpdb->blogs.slug,
`$wpdb->posts`.post_date_gmt,
`$wpdb->posts`.post_date,
 DATE_FORMAT( $wpdb->posts.post_date_gmt ,\"%a, %d %b %X %H:%i:%s +0000\") as rss_date,
`$wpdb->posts`.post_content as content
from `$wpdb->posts`
inner join `$wpdb->post2cat` on `$wpdb->posts`.ID = `$wpdb->post2cat`.post_id
inner join `$wpdb->categories` on `$wpdb->post2cat`.category_id = `$wpdb->categories`.cat_ID
inner join `$wpdb->blogs` on `$wpdb->categories`.blog =`$wpdb->blogs`.id
inner join  product_detail on product_detail.other = $wpdb->posts.ID
where $wpdb->posts.ID in ($id_list)
and `$wpdb->blogs`.status=\"active\"
and `$wpdb->posts`.post_status=\"publish\"
and TO_DAYS(NOW()- INTERVAL 9 HOUR ) - TO_DAYS( $wpdb->posts.post_date_gmt ) > -1
order by post_date_gmt desc $qry_num";
//order by product_detail.date desc $qry_num";


//DATE_FORMAT( $wpdb->posts.post_date_gmt  + interval 9 HOUR,\"%b %D, %Y\") as p_date

	$i=0;
      	$posts = $wpdb->get_results($my_q);
  		$posts = apply_filters('the_posts', $posts);

	if($posts){

		$output.= "\n";
	        foreach ($posts as $p){
			$i++;

			$title=$p->post_title;
    			mb_internal_encoding("UTF-8");//PEAK

			$url= "http://www.de-sci.org/blogs/".$p->slug ."/?p=".($p->ID);
			$profileuser = new WP_User($p->user_id);

			if($rss_date=="") $rss_date=$p->rss_date;
/*
http://www.petefreitag.com/item/465.cfm
<item>
<title>Title of an item</title>
<link>http://example.com/item/123</link>
<guid>http://example.com/item/123</guid>
<pubDate>Mon, 12 Sep 2005 18:37:00 GMT</pubDate>
<description>[CDATA[ This is the description. ]]</description>
</item>
*/			$content = $p->content;
			$output.= "<item>\n";
			$output.= "<title>".$profileuser->display_name.": $title </title>\n";
			$output.= "<link>$url</link>\n";
			$output.= "<guid>$url</guid>\n";
			$output.= "<pubDate>".$p->rss_date."</pubDate>\n";
			$output.= "<description><![CDATA[";

			$content= preg_replace("/<(\s*)span([^>]*)>/i", "<span>", $content);
			$content= preg_replace("/<(\s*)font([^>]*)>/i", "<font>", $content);
			$content= preg_replace("/<(\s*)p([^>]*)>/i", "<p>", $content);
			$content= preg_replace("/\n(\s*)\n/", "<br/><br/>\n", $content);
			$content=  preg_replace("/\n(\s*)\n/", "<br/><br/>\n", $content);
			$content= preg_replace("/<\/p>([\n\r\s]*)<br\/>/", "</p>", $content);

			$output.= $content;

			//$output.= concise_content($p->content,270,1);
			$output.= " ]]></description>\n";
			$output.= "</item>\n";

		}

	}

	return $output;//.$post_list;

}
?>
